# -*- coding: cp936 -*-
import sys, pygame, random
from pygame.locals import * 
import tkMessageBox
pygame.init()   

size = width, height = 480, 480
screen = pygame.display.set_mode((size))       

black, red, blue, white = (0, 0, 0), (255, 0, 0), (0,0, 255), (0,255,0)       
lose, tie, win = x, empty,o = -1, 0, 1 
human = o
board = [[empty] * 3, [empty] * 3, [empty] * 3] 

def draw_game():
    pygame.draw.rect(screen, white, (0, 0, width, height))

    pygame.draw.line(screen, black, (160, 0), (160, 480), 5)
    pygame.draw.line(screen, black, (320, 0), (320, 480), 5)
    pygame.draw.line(screen, black, (0, 160), (480, 160), 5)        
    pygame.draw.line(screen, black, (0, 320), (480, 320), 5)

    for row, line in enumerate(board):
        for col, val in enumerate(line):
            if val == x:

                upper_left = (col * 160 + 5, row * 160 + 5)
                lower_right = (col * 160 + 155, row * 160 + 155)
                pygame.draw.line(screen, blue, upper_left, lower_right, 5)

                upper_right = (col * 160 + 155, row * 160 + 5)
                lower_left = (col * 160 + 5, row * 160 + 155)
                pygame.draw.line(screen,blue, upper_right, lower_left, 5)
            elif val == o:
                rect = (col * 160 + 5, row * 160 + 5, 150, 150)
                pygame.draw.ellipse(screen, red, rect, 5)

    pygame.display.flip()

def is_won():
    for val in range(3):
        if board[0][val] == board[1][val] == board[2][val] != empty:
            return board[0][val]                                        
        if board[val][0] == board[val][1] == board[val][2] != empty:
            return board[val][0]
    if board[0][0] == board[1][1] == board[2][2] != empty:
        return board[1][1]                                      
    if board[0][2] == board[1][1] == board[2][0] != empty:
        return board[1][1]

    return empty

def calc_move(player):
        if all(val == empty for line in board for val in line):
            row = random.randrange(3)
            col = random.randrange(3)
            result = (tie, (row, col))
        else:
            result = best_move(player)
        return result

def best_move(player):
    best = None
    for row, line in enumerate(board):
        for col, val in enumerate(line):
            if val == empty:
                 board[row][col] = player
                 
                 if is_won() == player:
                    board[row][col] = empty
                    return (win, (row, col))
                 counter = best_move(player * -1)
                    
                 if best is None or best[0] <= (counter[0] * -1):
                    best = (counter[0] * -1, (row, col))
                 board[row][col] = empty

    if best is None: best = (tie, None)

    return best

def restart():
    global board, turn, human
    human *= -1
    turn = x
    draw_game()
    pygame.display.flip()

restart()
def EXIT():
          pygame.quit()
          exit(0)
while True:
    event = pygame.event.wait()

    pos = None

    if event.type == pygame.QUIT:
        pygame.quit()
        exit()
    elif event.type == KEYDOWN:
        if event.key == K_r:
            restart()
            draw_game()
            pygame.display.flip()
        elif event.key == K_q:
            pygame.event.post(pygame.event.Event(QUIT))
    elif event.type == MOUSEBUTTONDOWN and event.button == 1:
        pos = (event.pos[1] / 160, event.pos[0] / 160)

    if human != turn:
        pos = calc_move(turn)[1]

    if pos is None or is_won(): continue

    row, col = pos

    if board[row][col] == empty:
        board[row][col] = turn
        turn *= -1
        draw_game()
    if is_won() == -1:
            tkMessageBox.showinfo(title='win',message='You win')
            EXIT()
    elif is_won() == 1:
            tkMessageBox.showinfo(title='lose',message='You lose')
            exit()

